
import {
  AlipayCircleOutlined,
  TaobaoCircleOutlined,
  UserOutlined,
  WeiboCircleOutlined,
  LockOutlined
} from '@ant-design/icons';


import { login } from '@/services/user'; //导入接口
import { useEmotionCss } from '@ant-design/use-emotion-css';
import { history, SelectLang, useIntl, Helmet } from '@umijs/max';
import { Alert, message, Checkbox, Form, Input, Button, Card, Space, Flex } from 'antd';
import Settings from '../../../../config/defaultSettings';
import React, { useState } from 'react';
import "./index.css"



const ActionIcons = () => {
  const langClassName = useEmotionCss(({ token }) => {
    return {
      marginLeft: '8px',
      color: 'rgba(0, 0, 0, 0.2)',
      fontSize: '24px',
      verticalAlign: 'middle',
      cursor: 'pointer',
      transition: 'color 0.3s',
      '&:hover': {
        color: token.colorPrimaryActive,
      },
    };
  });

  return (
    <>
      <AlipayCircleOutlined key="AlipayCircleOutlined" className={langClassName} />
      <TaobaoCircleOutlined key="TaobaoCircleOutlined" className={langClassName} />
      <WeiboCircleOutlined key="WeiboCircleOutlined" className={langClassName} />
    </>
  );
};

const Lang = () => {
  const langClassName = useEmotionCss(({ token }) => {
    return {
      width: 42,
      height: 42,
      lineHeight: '42px',
      position: 'fixed',
      right: 16,
      borderRadius: token.borderRadius,
      ':hover': {
        backgroundColor: token.colorBgTextHover,
      },
    };
  });

  return (
    <div className={langClassName} data-lang>
      {SelectLang && <SelectLang />}
    </div>
  );
};

const LoginMessage: React.FC<{
  content: string;
}> = ({ content }) => {
  return (
    <Alert
      style={{
        marginBottom: 24,
      }}
      message={content}
      type="error"
      showIcon
    />
  );
};

const Login: React.FC = () => {
  const containerClassName = useEmotionCss(() => {
    return {
      display: 'flex',
      flexDirection: 'column',
      height: '100vh',
      overflow: 'auto',
      backgroundImage:
        "url('https://mdn.alipayobjects.com/yuyan_qk0oxh/afts/img/V-_oS6r-i7wAAAAAAAAAAAAAFl94AQBr')",
      backgroundSize: '100% 100%',
    };
  });

  const intl = useIntl();

  const handleSubmit = async (values: USERAPI.LoginParams) => {
    try {

      // 登录
      const msg = await login({ ...values });
      //token存好
      window.localStorage.setItem("token", msg.data.token);
      window.localStorage.setItem("userInfo", JSON.stringify(msg.data))
      
      if (true) {
        const defaultLoginSuccessMessage = intl.formatMessage({
          id: 'pages.login.success',
          defaultMessage: '登录成功！',
        });
        message.success(defaultLoginSuccessMessage);
        const urlParams = new URL(window.location.href).searchParams;
        history.push(urlParams.get('redirect') || '/list');
        return;
      }
      // 如果失败去设置用户错误信息
      //setUserLoginState(msg);
    } catch (error) {
      const defaultLoginFailureMessage = intl.formatMessage({
        id: 'pages.login.failure',
        defaultMessage: '登录失败，请重试！',
      });
      console.log(error);
      message.error(defaultLoginFailureMessage);
    }
  };


  return (
    <div className={containerClassName} >
      <Flex gap="small" wrap>
        <Button onClick={() => { window.location.href = '/landing'; }} type="primary" className="out-button" >返回</Button>

      </Flex>
      <Helmet>

        <title>
          {intl.formatMessage({
            id: 'menu.login',
            defaultMessage: '登录页',
          })}
          - {Settings.title}
        </title>
      </Helmet>
      <Lang />
      <div

        style={{
          display: 'flex',
          padding: '220px 0',
          alignItems: 'center',
          justifyContent: "center"
        }}
      >

        <Card title="数字码力实训项目" style={{ textAlign: "center", fontSize: "24px" }}>
          <Form
            name="normal_login"
            className="login-form"
            onFinish={handleSubmit}
          >
            <p >
              <Form.Item
                name="phone"
                rules={[{ required: true, message: '用户名不能为空' }]}
              >
                <Input prefix={<UserOutlined className="site-form-item-icon" />} placeholder="请输入用户名" />
              </Form.Item></p><p>
              <Form.Item
                name="password"
                rules={[{ required: true, message: '密码不能为空' }]}
              >
                <Input
                  prefix={<LockOutlined className="site-form-item-icon" />}
                  type="password"
                  placeholder="请输入密码"
                />
              </Form.Item></p>
              <p>
                <Form.Item>
                <Space style={{display:'flex' ,justifyContent: 'space-around'}}>
                    <Button type="primary" htmlType="submit" className="login-form-button">
                      登录
                    </Button>
                    <Button type="primary" className="login-form-button" onClick={() => history.push("/user/register")}>
                      注册
                    </Button>
                </Space>
               
                </Form.Item>
              </p>
          </Form>
        </Card>

      </div>

    </div>
  );
};

export default Login;
